$(function () {

    //sidebar dropdown menu
    jQuery('#sidebar .sub-menu > a').click(function () {
        var last = jQuery('.sub-menu.open', $('#sidebar'));
        last.removeClass("open");
        jQuery('.arrow', last).removeClass("open");
        jQuery('.sub', last).slideUp(200);
        var sub = jQuery(this).next();
        if (sub.is(":visible")) {
            jQuery('.arrow', jQuery(this)).removeClass("open");
            jQuery(this).parent().removeClass("open");
            sub.slideUp(200);
        } else {
            jQuery('.arrow', jQuery(this)).addClass("open");
            jQuery(this).parent().addClass("open");
            sub.slideDown(200);
        }
        var o = ($(this).offset());
        diff = 200 - o.top;
        //if (diff > 0)
        //    $("#sidebar").scrollTo("-=" + Math.abs(diff), 500);
        //else
        //    $("#sidebar").scrollTo("+=" + Math.abs(diff), 500);
    });

    //sidebar toggle
    $(function () {
        function responsiveView() {
            var wSize = $(window).width();
            if (wSize <= 768) {
                $('#container').addClass('sidebar-close');
                $('#sidebar > ul').hide();
            }

            if (wSize > 768) {
                $('#container').removeClass('sidebar-close');
                $('#sidebar > ul').show();
            }
        }

        $(window).on('load', responsiveView);
        $(window).on('resize', responsiveView);
    });

    $('.icon-reorder').click(function () {
        if ($('#sidebar > ul').is(":visible") === true) {
            $('#main-content').css({
                'margin-left': '0px'
            });
            $('#sidebar').css({
                'margin-left': '-180px'
            });
            $('#sidebar > ul').hide();
            $("#container").addClass("sidebar-closed");
        } else {
            $('#main-content').css({
                'margin-left': '180px'
            });
            $('#sidebar > ul').show();
            $('#sidebar').css({
                'margin-left': '0'
            });
            $("#container").removeClass("sidebar-closed");
        }
    });

    //custom scrollbar
    $("#sidebar").niceScroll({
        styler: "fb",
        cursorcolor: "#e8403f",
        cursorwidth: '3',
        cursorborderradius: '10px',
        background: '#404040',
        cursorborder: ''
    });

    $(".panel-body").niceScroll({
        styler: "fb",
        cursorcolor: "#e8403f",
        cursorwidth: '6',
        cursorborderradius: '10px',
        background: '#404040',
        cursorborder: '',
        zindex: '1000'
    });

    //widget tools
    jQuery('.widget .tools .icon-chevron-down').click(function () {
        var el = jQuery(this).parents(".widget").children(".widget-body");
        if (jQuery(this).hasClass("icon-chevron-down")) {
            jQuery(this).removeClass("icon-chevron-down").addClass("icon-chevron-up");
            el.slideUp(200);
        } else {
            jQuery(this).removeClass("icon-chevron-up").addClass("icon-chevron-down");
            el.slideDown(200);
        }
    });

    jQuery('.widget .tools .icon-remove').click(function () {
        jQuery(this).parents(".widget").parent().remove();
    });

    //tool tips
    $('.tooltips').tooltip();

    //popovers
    $('.popovers').popover();


    //custom bar chart
    if ($(".custom-bar-chart")) {
        $(".bar").each(function () {
            var i = $(this).find(".value").html();
            $(this).find(".value").html("");
            $(this).find(".value").animate({
                height: i
            }, 2000)
        })
    }

    //商品类别
    $("#goodType").click(function(){
       addTab("goodTypePage","商品类别",ctx+"/goodType/page.do");
    });
    //店铺认证
    $("#shopAuthentication").click(function(){
        addTab("shopManageAuthenticationPage","店铺认证",ctx+"/shop/shopManageAuthenticationPage.do");
    });
    $('#main-content>.wrapper').css('height', parseInt($('#main-content').css('height')) - parseInt($('#main-content>.wrapper').css('margin-top')) + "px");
    setTimeout(function(){
        $('.panel-body').css("height", parseInt($('#main-content>.wrapper').css("height")) - $('#main-content>.wrapper>div>section>header').outerHeight());
    },1000);

    window.onresize = function(){
        $('#main-content>.wrapper').css('height', parseInt($('#main-content').css('height')) - parseInt($('#main-content>.wrapper').css('margin-top')) + "px");
        setTimeout(function(){
            $('.panel-body').css("height", parseInt($('#main-content>.wrapper').css("height")) - $('#main-content>.wrapper>div>section>header').outerHeight());
        },1000);
    }
});

var tabIds = ['home'];
function addTab(tabId, title, url) {
    tabIds.push(tabId);
    if ($('[href="#' + tabId + '"]').length > 0)
        $('[href="#' + tabId + '"]').tab('show');
    else {
        $.ajax({
            url: url,
            dataType: 'html',
            success: function (document) {
                $('#mainTab').append('<li>' +
                    '<a data-toggle="tab" href="#' + tabId + '">' +
                    title +
                    '<i class="fa fa-times tabclose" onclick="onTabCloseClick(\'' + tabId + '\')" aria-hidden="true"></i>' +
                    '</a>' +
                    '</li>');
                $('#mainContent').append('<div id="' + tabId + '" class="tab-pane">' + '</div>');
                $('#' + tabId).html(document);
                $('[href="#' + tabId + '"]').tab('show');
            }
        });
    }
}

function onTabCloseClick(tabId) {
    var newTabIds = [];
    for(var i=0;i<tabIds.length;i++){
        if(tabIds[i]!=tabId)
            newTabIds.push(tabIds[i]);
    }
    tabIds = newTabIds;
    $('[href="#' + tabId + '"]').remove();
    $('#' + tabId).remove();
    $('[href="#'+tabIds[tabIds.length-1]+'"]').tab('show');
}

function showTab(tabId) {
    $('[href="#' + tabId + '"]').tab('show');
}